(function () {

    return {
        init:function(){
            var _this = this;
            if (_this.params.sysUserId == null) {
                DsModule.to("module/sysUser/index");
                return false;
            }
            ajaxPost({
                url: "sysUser/findSysRoleBySysUserId",
                data: {sysUserId: _this.params.sysUserId},
                success: function (data) {
                    _this.data.sysRole = data.data;
                    _this.reload();
                }
            });
        },
        data:{
            sysRole:{},
            sysRoleMap:{}
        },
        before: function (data) {
            for(var k in data.sysRole){
                _this.data.sysRoleMap[_this.data.sysRole[k].id]=_this.data.sysRole[k];
            }
            return data;
        },
        after: function () {
            var _this = this;
            this.$(".e-search").click(function () {
                var query = _this.toMap(".search-param");
                _this.$(".bootstrapTable").bootstrapTable("refresh", {"query": query});
            });

            bootstrapTable(_this.$(".bootstrapTable"), {
                    url: 'sysRole/findPage',
                    columns: [
                        {
                            field: 'selectItem',
                            checkbox: true,
                            formatter:function(val,row,index){
                                if(_this.data.sysRoleMap[row.id]==null){
                                    return {
                                        disabled : false,//设置是否可用
                                        checked : false//设置选中
                                    }
                                }
                                return{
                                    disabled : false,//设置是否可用
                                    checked : true//设置选中
                                }
                            }
                        },
                        {
                            field: 'name',
                            title: '角色名'
                        }, {
                            field: 'createDate',
                            title: '创建时间'
                        }
                    ],
                    onCheck:function(row){
                        ajaxPost({
                            url:"sysUser/setSysRole",
                            data:{"sysUserId":_this.params.sysUserId,"sysRoleIds":row.id},
                            success:function(data){
                                _this.data.sysRoleMap[row.id]=row;
                                toastr.success("设置角色成功");
                            }
                        })
                    },
                    onUncheck:function(row){
                        ajaxPost({
                            url:"sysUser/unSetSysRole",
                            data:{"sysUserId":_this.params.sysUserId,"sysRoleIds":row.id},
                            success:function(data){
                                _this.data.sysRoleMap[row.id]=null;
                                toastr.success("取消角色成功");
                            }
                        })
                    },
                    onCheckAll:function(rows){
                        var sysRoleIds=[];
                        for(var i=0;i<rows.length;i++){
                            sysRoleIds.push(rows[i].id);
                        }
                        ajaxPost({
                            url:"sysUser/setSysRole",
                            data:{"sysUserId":_this.params.sysUserId,"sysRoleIds":sysRoleIds.join(",")},
                            success:function(data){
                                toastr.success("设置角色成功");
                            }
                        })

                    },
                    onUncheckAll:function(rows){
                        var sysRoleIds=[];
                        for(var i=0;i<rows.length;i++){
                            sysRoleIds.push(rows[i].id);
                        }
                        ajaxPost({
                            url:"sysUser/setSysRole",
                            data:{"sysUserId":_this.params.sysUserId,"sysRoleIds":sysRoleIds.join(",")},
                            success:function(data){
                                toastr.success("设置角色成功");
                            }
                        })
                    },
                    onLoadSuccess: function () {

                    }
                }
            );
        }
    }
});